//
//  HMRemindList.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/8/9.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

protocol HMRemindListDelegate: class {
   func remindList(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
}


class HMRemindList: HMTableViewController {
    var dataAry: Array<Any>!
    var cellHeight: CGFloat = 44 // 默认 44高度
    var reminderJson = JSON()
    weak var delegate : HMRemindListDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NetRequestTool.request(url: "user/medicine/reminder", parameters: [:], success: { (reminderJson) in
            print("====>>", reminderJson)
            self.reminderJson = reminderJson["data"]
            self.tableView.reloadData()
        }, fail: { (code, err) in
            print("====>>失败")
        }) {
            
        }
        
    }
    
    override func load() {
        self.dataAry = ["消息通知","修改手机绑定","帮助"]
//        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.tableView.register(HMRemindCell.classForCoder(), forCellReuseIdentifier: "HMRemindCell")
    }
    
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
//    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        return 44
//    }
//    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let view = UIView()
//        view.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
//        view.backgroundColor = UIColor.gray
//        return view
//    }
    

    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.reminderJson.count// 1
    }
   // cell 数据
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let newCell = cell as! HMRemindCell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
//        print("=======>heightForRowAt ->\(indexPath.row)")
        let cell = tableView.cellForRow(at: indexPath) //as! HMRemindCell
        return self.cellHeight //3
    }

    override func cellForRow(atIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        
        let cell = HMRemindCell.cell(tableView: self.tableView, cellForRowAt: indexPath as IndexPath)
        self.cellHeight = (cell?.cellHeight)!

        cell?.setJson(json: nil)
        
//        print("cellForRow ->\(indexPath.row)--->cell: \(self.cellHeight)")
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if delegate != nil {
          delegate?.remindList(tableView, didSelectRowAt: indexPath)
        }
        
    }
    
    
    
}
